﻿using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Willow.WebApplication.Hubs
{
    public class ChatRoom
    {

        private readonly static Lazy<ChatRoom> _instance = new Lazy<ChatRoom>(() => new ChatRoom());
        private readonly ConcurrentDictionary<string, ChatUser> users = new ConcurrentDictionary<string, ChatUser>();
        private readonly IHubContext _hubContext;
        public static ChatRoom Instance
        {
            get
            {
                return _instance.Value;
            }
        }
        private ChatRoom()
        {
            _hubContext = GlobalHost.ConnectionManager.GetHubContext<ChatHub>();
        }

        public void Online(string connectionId, ChatUser chatUser)
        {
            users.GetOrAdd(connectionId, chatUser);
        }

        public void OffLine(string connectionId)
        {
            ChatUser user;
            users.TryRemove(connectionId, out user);
        }

        public IEnumerable<ChatUser> GetAllUsers()
        {
            return users.Values;
        }

        public ChatUser GetUser(string conntectId)
        {
            return users[conntectId];
        }

    }
    public class ChatUser
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public string OrgId { get; set; }

    }
}